package com.example.factory.core.service.impl;

import com.example.factory.core.domain.Department;
import com.example.factory.core.repository.DepartmentRepository;
import com.example.factory.core.service.DepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Lei Tao
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    private final DepartmentRepository departmentRepository;

    public DepartmentServiceImpl(DepartmentRepository departmentRepository) {
        this.departmentRepository = departmentRepository;
    }

    @Override
    @Transactional
    public void save(Department department) {
        departmentRepository.save(department);
    }

    @Override
    @Transactional
    public void deleteDepartment(String departmentGuid) {
        Department department = departmentRepository.findByGuidAndArchivedFalse(departmentGuid);
        department.archived();
        departmentRepository.save(department);
    }

    @Override
    public Department findDepartment(String departmentGuid) {
        return departmentRepository.findByGuidAndArchivedFalse(departmentGuid);
    }
}
